package com.hddz.acquisition.activity.stock;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.hddz.acquisition.R;
import com.hddz.acquisition.activity.BaseActivity;
import com.hddz.acquisition.fragment.stock.FragmentGroundingFree1;
import com.hddz.acquisition.fragment.stock.FragmentGroundingScan;
import com.hddz.acquisition.model.JsonRespose;

@ContentView(R.layout.activity_stock_grounding)
public class GroundingActivity extends BaseActivity {
    private final String TAG = "GroundingActivity";
    @ViewInject(R.id.tab_vpager)
    private ViewPager tab_vpager;
    @ViewInject(R.id.lbl_main_title)
    private TextView lbl_main_title;
    @SuppressLint("UseSparseArrays")
    private Map<Integer, Fragment> mCacheFragments = new HashMap<Integer, Fragment>();
    private Fragment mFm = null;
    FragmentGroundingScan fragmentGroundingScan = null;
    FragmentGroundingFree1 fragmentGroundingFree1 = null;

    String targetwarehouse = "";


    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        lbl_main_title.setText(R.string.menu_spsj);
        tab_vpager.setAdapter(new MainViewPagerAdapter(
                getSupportFragmentManager()));
        tab_vpager.setOffscreenPageLimit(2);
        baseSendCommand("1101", "");
    }

    @Override
    public void addResponse(JsonRespose json) {
        if (currentCmd.equals("1101")) {

        } else if (currentCmd.equals("1102")) {
            targetwarehouse = json.response.cTargetWarehouse;
            fragmentGroundingScan.updateResponse(json);
        } else if (currentCmd.equals("1103")) {
            fragmentGroundingScan.updateResponse(json);
            fragmentGroundingFree1
                    .updateList(json.response.ResponseList);
        } else if (currentCmd.equals("1104")) {
            Toast.makeText(GroundingActivity.this, "保存成功!",
                    Toast.LENGTH_SHORT).show();
            finish();
        } else if (currentCmd.equals("1105")) {
            finish();
        }
        fragmentGroundingScan.appandMsg(json.message);
    }

    @Override
    public void addErr(String err) {
        fragmentGroundingScan.appandMsg(err);
    }

    @Override
    public void receiveCode(String code) {
        fragmentGroundingScan.updateBarcode(code);
        if (currentCmd.equals("1101")) {
            baseSendCommand("1102", "","", code);
        } else if (currentCmd.equals("1102")
                || currentCmd.equals("1103")
                || currentCmd.equals("1106")) {
            if (fragmentGroundingScan.cb_unActive.isChecked()) {
                fragmentGroundingScan.cb_unActive.setChecked(false);
                baseSendCommand("1106", code);
            } else {
                baseSendCommand("1103", code);
            }
        }
    }

    @Override
    public void backDo() {
        onBack(null);
    }

    @Override
    public void finishDo() {
        onFinish(null);
    }

    private class MainViewPagerAdapter extends FragmentPagerAdapter {

        public MainViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return 2;
        }

        @Override
        public Fragment getItem(int position) {
            if (mCacheFragments.containsKey(position)) {
                mFm = mCacheFragments.get(position);
            } else {
                switch (position) {
                    case 0:
                        fragmentGroundingScan = FragmentGroundingScan.newInstance();
                        mFm = fragmentGroundingScan;
                        break;
                    case 1:
                        fragmentGroundingFree1 = FragmentGroundingFree1
                                .newInstance();
                        mFm = fragmentGroundingFree1;
                        break;
                    default:
                        break;
                }
                mCacheFragments.put(position, mFm);
            }
            return mFm;
        }
    }


    @Event(R.id.btn_back)
    private void onBack(View view) {
        if (targetwarehouse.equals("")) {
            finish();
        } else {

            dialogHelper.showAlertDialog("是否取消操作?", 24, new OnClickListener() {

                @Override
                public void onClick(DialogInterface arg0, int arg1) {
                    // TODO Auto-generated method stub
                    baseSendCommand("1105", "");
                }
            });
        }
    }

    @Event(R.id.btn_finish)
    private void onFinish(View view) {
        if (targetwarehouse.equals("")) {
            dialogHelper.showAlertDialog("未扫描架子！", 24);
        } else {
            baseSendCommand("1104", "");
        }
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        // TODO Auto-generated method stub
        if (event.getAction() == KeyEvent.ACTION_UP) {
            switch (event.getKeyCode()) {
                case KeyEvent.KEYCODE_F1:
                    fragmentGroundingScan.cb_unActive.setChecked(true);
                    return true;
            }
        }
        return super.dispatchKeyEvent(event);
    }
}
